package com.zpy.SmartChargeStation.mqtt.client.factory;

import com.zpy.SmartChargeStation.mqtt.client.conf.MqttProps;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;

/**
 * description: MQTT 工厂类
 */


@Configuration
@Slf4j
public class FactoryBuilder {

    @Resource
    private MqttProps props;


    /**
     * description: 获取Mqtt客户端
     */
    @Bean
    public MqttPahoClientFactory getFactory() {

        DefaultMqttPahoClientFactory client =
                new DefaultMqttPahoClientFactory();

        MqttConnectOptions options = props.getOptions();

        // 如果options为null，则创建一个新的实例
        if (options == null) {
            options = new MqttConnectOptions();
        }

        //配置连接地址
        options.setServerURIs(new String[]{props.getHost()});

        client.setConnectionOptions(options);

        return client;

    }
}
